package tencentcloud

import (
	"context"
	"github.com/spf13/viper"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	iai "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/iai/v20200303"
	"go.uber.org/zap"
	"golang-sgg-daijia/internal/model"
	"golang-sgg-daijia/pkg/log"
	"strconv"
)

type IaiClient interface {
	CreatePerson(ctx context.Context,
		driverInfo *model.DriverInfo,
		imageBase64 string) (*iai.CreatePersonResponse, error)
	VerifyFace(ctx context.Context, imageBase64 string, driverID int64) (bool, error)
	DetectLiveFace(ctx context.Context, imageBase64 string) (bool, error)
}

type iaiClient struct {
	logger *log.Logger
	conf   *viper.Viper
	iaicli *iai.Client
}

func NewIaiClient(
	logger *log.Logger,
	conf *viper.Viper,
	iaicli *iai.Client,
) IaiClient {
	return &iaiClient{
		logger: logger,
		conf:   conf,
		iaicli: iaicli,
	}
}

func NewIaiCli(conf *viper.Viper) *iai.Client {
	credential := common.NewCredential(
		conf.GetString("security.tencentclould.secret_Id"),
		conf.GetString("security.tencentclould.secret_key"),
	)
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "iai.tencentcloudapi.com"

	client, err := iai.NewClient(credential, conf.GetString("security.tencentclould.region"), cpf)
	if err != nil {
		panic(err)
	}
	return client
}

func (a *iaiClient) CreatePerson(ctx context.Context,
	driverInfo *model.DriverInfo,
	imageBase64 string) (*iai.CreatePersonResponse, error) {

	request := iai.NewCreatePersonRequest()
	groupId := a.conf.GetString("security.tencentclould.person_group_id")
	request.GroupId = &groupId
	persionId := strconv.Itoa(int(driverInfo.ID))
	request.PersonId = &persionId
	gender, _ := strconv.ParseInt(driverInfo.Gender, 10, 64)
	request.Gender = &gender
	var qualityControl uint64 = 3
	request.QualityControl = &qualityControl
	var uniquePersonControl uint64 = 3
	request.UniquePersonControl = &uniquePersonControl
	request.PersonName = &driverInfo.Name
	request.Image = &imageBase64
	resp, err := a.iaicli.CreatePersonWithContext(ctx, request)
	if err != nil {
		a.logger.WithContext(ctx).Error("iaiClient|CreatePerson|CreatePersonWithContext", zap.Error(err))
		return nil, err
	}
	return resp, nil
}

func (a *iaiClient) VerifyFace(ctx context.Context, imageBase64 string, driverID int64) (bool, error) {
	req := iai.NewVerifyFaceRequest()
	req.Image = &imageBase64
	driverIDStr := strconv.Itoa(int(driverID))
	req.PersonId = &driverIDStr
	resp, err := a.iaicli.VerifyFaceWithContext(ctx, req)
	if err != nil {
		a.logger.WithContext(ctx).Error("iaiClient|VerifyFace|VerifyFaceWithContext", zap.Error(err))
		return false, err
	}
	return *resp.Response.IsMatch, nil

}

func (a *iaiClient) DetectLiveFace(ctx context.Context, imageBase64 string) (bool, error) {
	req := iai.NewDetectLiveFaceRequest()
	req.Image = &imageBase64

	resp, err := a.iaicli.DetectLiveFaceWithContext(ctx, req)
	if err != nil {
		a.logger.WithContext(ctx).Error("iaiClient|DetectLiveFace|DetectLiveFaceWithContext", zap.Error(err))
		return false, err
	}
	return *resp.Response.IsLiveness, nil

}
